package com.pengy.shengxian;

import javafx.application.Application;
import javafx.application.Platform;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;

@SpringBootApplication
public class ShengxianApplication extends Application {

    private static final Logger logger = LoggerFactory.getLogger(ShengxianApplication.class);

    private ConfigurableApplicationContext springContext;
    private Parent rootNode;

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void init() throws Exception {
        springContext = SpringApplication.run(ShengxianApplication.class);
        // 获取当前激活的环境配置
        String activeProfile = springContext.getEnvironment().getActiveProfiles()[0];
        String url = springContext.getEnvironment().getProperty("spring.datasource.url");
        logger.info("Spring 启用了环境配置: " + activeProfile);
        logger.info("Spring 数据库连接: " + url);
        rootNode = springContext.getBean(MainView.class).getRootNode();
    }

    @Override
    public void start(Stage primaryStage) throws Exception {
        // 设置窗口标题
        primaryStage.setTitle("生鲜数据管理系统 v1.0");

        // 创建场景并加载CSS样式
        Scene scene = new Scene(rootNode);
        scene.getStylesheets().add(getClass().getResource("/css/styles.css").toExternalForm());

        // 设置场景
        primaryStage.setScene(scene);

        // 设置窗口大小
        primaryStage.setMinWidth(1200);
        primaryStage.setMinHeight(800);
        primaryStage.setWidth(1400);
        primaryStage.setHeight(900);

        // 设置窗口居中显示
        primaryStage.centerOnScreen();

        // 显示窗口
        primaryStage.show();

        System.out.println("生鲜数据管理系统启动成功！");
        System.out.println("窗口大小: " + primaryStage.getWidth() + "x" + primaryStage.getHeight());
    }

    @Override
    public void stop() throws Exception {
        if (springContext != null) {
            springContext.close();
        }
        Platform.exit();
    }
}